package com.qdlc.p2p.biz.task;


import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.qdlc.p2p.biz.task.impl.*;
import com.qdlc.p2p.dal.dto.Notice;

public class JobQueue<T> {

    private Queue<T> queue = new ConcurrentLinkedQueue<T>();
    private Lock LOCK = new ReentrantLock();
    private LoanTask task;

    private static class LazyHolder {
        // 通知信息队列
        private static final JobQueue<Notice> NOTICE_INSTANCE = new JobQueue<Notice>(new NoticeTask());
        // 投标相关队列
        private static final JobQueue<ValueEvent> TENDER_INSTANCE = new JobQueue<ValueEvent>(new TenderTask());
        // 标相关队列
        private static final JobQueue<ValueEvent> BORROW_INSTANCE = new JobQueue<ValueEvent>(new BorrowTask());
        // 标审核相关队列
        private static final JobQueue<ValueEvent> VERIFY_BORROW_INSTANCE = new JobQueue<ValueEvent>(new VerifyBorrowTask());
        // 充值提现相关队列
        private static final JobQueue<ValueEvent> USER_INSTANCE = new JobQueue<ValueEvent>(new UserTask());
        // 债权相关队列
        private static final JobQueue<ValueEvent> BOND_INSTANCE = new JobQueue<ValueEvent>(new BondTask());
        // 第三方投标业务处理队列
        private static final JobQueue<ValueEvent> TPP_INVEST_INSTANCE = new JobQueue<ValueEvent>(new TppInvestTask());

        // 第三方代收代付交易业务处理队列
        private static final JobQueue<ValueEvent> TPP_TRADE = new JobQueue<ValueEvent>(new TppTradeTask());

        //趣享发放奖品业务处理队列
        private static final JobQueue<ValueEvent> ENJOY_GOODS=new JobQueue<ValueEvent>(new EnjoyGoodsTask());

        // 购票业务处理队列
        private static final JobQueue<ValueEvent> GET_BUY_MOVIE_TICKETS_INSTANCE = new JobQueue<ValueEvent>(new BuyMovieTicketsInstanceTask());
        
        // 自动调度任务处理队列
		private static final JobQueue<ValueEvent> TPP_DISPATCH = new JobQueue<ValueEvent>(new TppDispatchTask());
    }

    /**
     * 通知队列实例化
     *
     * @return
     */
    public static JobQueue<Notice> getNoticeInstance() {
        return LazyHolder.NOTICE_INSTANCE;
    }

    /**
     * 投标队列实例化
     *
     * @return
     */
    public static JobQueue<ValueEvent> getTenderInstance() {
        return LazyHolder.TENDER_INSTANCE;
    }

    /**
     * 标队列实例化
     *
     * @return
     */
    public static JobQueue<ValueEvent> getBorrowInstance() {
        return LazyHolder.BORROW_INSTANCE;
    }

    /**
     * 标审核处理
     *
     * @return
     */
    public static JobQueue<ValueEvent> getVerifyBorrowInstance() {
        return LazyHolder.VERIFY_BORROW_INSTANCE;
    }

    /**
     * 购票业务处理
     * @return
     */
    public static JobQueue<ValueEvent> getBuyMovieTicketsInstance() {
        return LazyHolder.GET_BUY_MOVIE_TICKETS_INSTANCE;
    }

    /**
     * 用户相关处理：注册开户，绑定银行卡等
     *
     * @return
     */
    public static JobQueue<ValueEvent> getUserInstance() {
        return LazyHolder.USER_INSTANCE;
    }

    /**
     * 债权处理
     *
     * @return
     */
    public static JobQueue<ValueEvent> getBondInstance() {
        return LazyHolder.BOND_INSTANCE;
    }

    /**
     * 第三方代收代付交易业务处理
     *
     * @return
     */
    public static JobQueue<ValueEvent> getTppTradeInstance() {
        return LazyHolder.TPP_TRADE;
    }

    /**
     * 第三方放款业务处理
     *
     * @return
     */
    public static JobQueue<ValueEvent> getTppInvestInstance() {
        return LazyHolder.TPP_INVEST_INSTANCE;
    }

    /**
     *趣享发放奖品业务处理
     */
    public static JobQueue<ValueEvent> getEnjoyGoodsInstance() {
        return LazyHolder.ENJOY_GOODS;
    }


    private JobQueue(LoanTask task) {
        super();
        this.task = task;
    }


    public void offer(T model) {
        if (!queue.contains(model)) {
            queue.offer(model);
            try {
                LOCK.lock();
                task.execute();
            } finally {
                LOCK.unlock();
            }

        }
    }

    public void offer(List<T> ts) {
        for (int i = 0; i < ts.size(); i++) {
            T t = ts.get(i);
            if (!queue.contains(t)) {
                queue.offer(t);
            }
        }
        try {
            LOCK.lock();
            task.execute();
        } finally {
            LOCK.unlock();
        }
    }

    public T poll() {
        return queue.poll();
    }

    public T peek() {
        return queue.peek();
    }

    public void remove(T model) {
        queue.remove(model);
    }

    public int size() {
        return queue.size();
    }

    public boolean isEmpty() {
        try {
            LOCK.lock();
            return queue.isEmpty();
        } finally {
            LOCK.unlock();
        }

    }

    public void stop() {
        task.stop();
    }

    /**
     * 自动调度任务业务处理
     */
	public static JobQueue<ValueEvent> getTppDispatchInstance() {
		return LazyHolder.TPP_DISPATCH;
	}

}
